# -*- coding: UTF-8 -*-
__author__ = 'Administrator'
from Config import *
import os


Tab_Str = "    "

class LuaCodeGenerator:
    def __init__(self, parser=None):
        self.parser = parser
        self.table_define_path = os.path.join(output_lua_folder, 'Resources/Table/TABLE_FIELD_DEFINE.lua')
        self.tab_count = 0

    def get_fields(self):
        return self.parser.get_fields()

    def get_data(self):
        return self.parser.get_data()

    def pre_generate_field(self):
        with open(self.table_define_path, 'wt', encoding='utf-8') as file:
            file.write("TableDefine = \n")
            file.write("{\n")

    def generate_field(self):
        with open(self.table_define_path, 'at', encoding='utf-8') as file:
            file.write("    %s = \n" % self.parser.get_sheet_name())
            file.write("    {\n")
            for field in self.get_fields():
                self.generate_field_with_type(field, file, 2)
            file.write("    }\n")

    def generate_field_with_type(self, field, file, tab_count):
        if field.get_type() == "array":
            self.generate_arr_field(field, file, tab_count)
        else:
            self.generate_normal_field(field, file, tab_count)

    def generate_normal_field(self, field, file, tab_count):
        file.write("%s%-10s=%4s          ---%s\n" % (tab_count * Tab_Str, field.get_name(), field.get_col_index(), field.get_desc()))

    def generate_arr_field(self, field, file, tab_count):
        file.write("%s%-10s=              ---%s\n" % (tab_count * Tab_Str, field.get_name(), field.get_desc()))
        file.write("%s{\n" % (tab_count * Tab_Str))
        for struct_fields in field.get_fields():
            file.write("%s{\n" % ((tab_count + 1) * Tab_Str))
            for child_field in struct_fields:
                self.generate_field_with_type(child_field, file, tab_count + 2)
            file.write("%s}\n" % ((tab_count + 1) * Tab_Str))
        file.write("%s}\n" % (tab_count * Tab_Str))

    def post_generate_field(self):
        with open(self.table_define_path, 'at', encoding='utf-8') as file:
            file.write("}")

    def generate_data(self):
        table_data_path = os.path.join(output_lua_folder, 'Resources/Table', "%s.lua" % self.parser.get_sheet_name())
        with open(table_data_path, 'wt', encoding='utf-8') as file:
            file.write("DataTableSet.%s = \n" % self.parser.get_sheet_name())
            file.write("{\n")
            for row_data in self.get_data():
                file.write("    {")
                for property_data in row_data:
                    if isinstance(property_data, int):
                        file.write("%s," % property_data)
                    elif isinstance(property_data, str):
                        file.write("'%s'," % property_data)
                    elif isinstance(property_data, list):
                        file.write("%s," % property_data[0])
                    else:
                        file.write("'%s'," % property_data)
                file.write("},\n")
            file.write("}\n")